package com.qf.controller;

import com.qf.form.SmsInterfaceForm;
import com.qf.service.InterfaceService;
import com.qf.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WuZiYu
 * @date 2022-6-2 下午 10:28
 *
 * 映射请求路径：http://localhost/sms/interface
 */
@RestController
@RequestMapping("/sms")
public class interfaceController {

    /**
     * 获取Gitee配置文件中nginx代理的ip地址, 请求头信息:
     */
    @Value("${ip-header}")
    private List<String> ipHeaders;

    @Autowired
    private InterfaceService interfaceService;

    /**
     * 接收客户端的发短信
     */
    @RequestMapping("/interface")
    public Map smsInterface(SmsInterfaceForm smsInterfaceForm, HttpServletRequest request){

        //客户端的IP地址
        String ip = request.getRemoteAddr();
        smsInterfaceForm.setIp(ip);

        //1客户端传递的SmsInterfaceForm 里面属性  进行非空校验
        Map result = checkNotNull(smsInterfaceForm);
        if (result != null){
            //有空值
            return result;
        }
        //2客户端传递的SmsInterfaceForm 里面属性  进行非空校验
        result = interfaceService.check(smsInterfaceForm);

        return result;
    }

    private Map checkNotNull(SmsInterfaceForm smsInterfaceForm) {
        Map resultMap = new HashMap();
        String clientID = smsInterfaceForm.getClientID();
        if (StringUtils.isEmpty(clientID)){
            resultMap.put("code","101");
            resultMap.put("msg","clientId不能为空");
            return resultMap;
        }
        if (StringUtils.isEmpty(smsInterfaceForm.getPwd())) {
            resultMap.put("code", "102");
            resultMap.put("msg", "密码错误");
            return resultMap;
        }
        if (StringUtils.isEmpty(smsInterfaceForm.getIp())) {
            resultMap.put("code", "103");
            resultMap.put("msg", "IP校验错误");
            return resultMap;
        }
        if (StringUtils.isEmpty(smsInterfaceForm.getContent())) {
            resultMap.put("code", "104");
            resultMap.put("msg", "消息长度错，为空或超长（目前定为500个字）");
            return resultMap;
        }
        if (StringUtils.isEmpty(smsInterfaceForm.getMobile())) {
            resultMap.put("code", "105");
            resultMap.put("msg", "手机号错误");
            return resultMap;
        }
        if (StringUtils.isEmpty(smsInterfaceForm.getSrcID())) {
            resultMap.put("code", "106");
            resultMap.put("msg", "下行编号（srcID）错误");
            return resultMap;
        }
        return null;
    }
    /**
     * 从请求中获取并校验ip地址是否正确
     * @param req
     * @return
     */
    private String getReallyIpAddress(HttpServletRequest req) {
        for (String ipHeader : ipHeaders) {
            //1. 校验请求头不为空
            if(!StringUtils.isEmpty(ipHeader) && ipHeader.length() > 0){
                //2. 获取真实的ip地址
                String ip = req.getHeader(ipHeader);
                //3. 判断是否真的拿到了ip地址
                if(!StringUtils.isEmpty(ip) && !"unknown".equalsIgnoreCase(ip)){
                    //4. 获取到了真实的ip
                    return ip;
                }
            }
        }
        //5. 循环结束,没有拿到真实的ip地址,通过传统的方式获取ip
        return req.getRemoteAddr();
    }
    }